﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PresentationLayer.Presenters
{
    using WinFormsMvp;
    using PresentationLayer.Views.ViewContracts;
    using EngineLayer;
    using System.Data;
    using System.Windows.Forms;
    using PresentationLayer.Properties;

    public class ProductsCatalogsPresenter : Presenter<ICommonCatalogView>
        {
            public ProductsCatalogsPresenter(ICommonCatalogView view)
                : base(view)
            {
                View.GetCatalogData += View_GetCatalogData;
                View.GetComboGridData += View_GetComboGridData;
                View.TextBoxFindChanged += View_TextBoxFindChanged;
                View.DataGridViewError += View_DataGridViewError;
                View.DataGridViewCellContentClick += View_DataGridViewCellContentClick;
                View.OnClickButton += View_OnClickButton; 
                View.Load += View_Load;
            }

            private void View_DataGridViewCellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                ///((DataGridView)sender).ReadOnly = false;
            }

            private void View_OnClickButton(object sender, EventArgs e)
            {
                string OptionSelected = ((string)((Button)sender).Tag);
                 
                if(OptionSelected ==  Resources.Cancelar)
                        View.CloseForm();
                else if(OptionSelected == Resources.Actualizar)
                {
                      // View.Model.OnlineUpdate();
                       //View.CloseForm();
                    
                    //  if(OptionSelected ==  Resources.Agregar)
                    
                    //if(OptionSelected ==  Resources.Actualizar)
                
                
                }
                 

            }

            private void View_TextBoxFindChanged(object sender, EventArgs e)
            {
            //   View.Binding(View.Model.ProductService().FilterByProductNameOrID(((TextBox)sender).Text));
            }

            private void View_DataGridViewError(object sender, DataGridViewDataErrorEventArgs anError)
            {
                if ((anError.Exception) is ConstraintException) anError.ThrowException = false;
             
               
            }
           
            private void View_GetCatalogData(object sender, EventArgs e)
            {

                //var results4 = (from category in View.Model.Libraries.Categories.ToList()
                //              join product in View.Model.Libraries.Products.ToList()
                //              on category.Id equals product.Category
                //              select new 
                //              { 
                //                  Id = product.Id,
                //                  Name = product.Name,
                //                  Description = product.Description,
                //                  Price = product.Price,
                //                  Public = product.Public,
                //                  CategoryID = product.Category,
                //                  Category = category
                //              }).OrderBy(order => order.Name);

            //    var results = View.Model.Libraries.Products.ToList().
            //        Join(View.Model.Libraries.Categories.ToList(), p => p.Category

                View.Binding(View.Model.Libraries.Products.ToList());
                
            }

            private void View_GetComboGridData(object sender, EventArgs e)
            {

                View.ComboGridInfo(View.Model.Libraries.Categories.ToList());
                
            }

            private void View_Load(object sender, EventArgs e)
            {

                View.Model = Program.application;
               
                //ProductsCatalogsModel();
                
            }
         


        }
    }


